package learnJSP;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowParametersServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unchecked")
	public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title> Show Parameters by Servlet");
		out.println("</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("You passed the Address Bar the following parameters: <pre>");
		Enumeration params = request.getParameterNames();

		while (params.hasMoreElements()) {
			String paramName = params.nextElement().toString();
			String paramValues [] = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				out.println(paramName + "=" + paramValues[0]);
			}
			else {
				out.print(paramName + "=");
				for (int i=0; i<paramValues.length; i++) {
					out.print(paramValues[i] + ",");
				}
				out.println();
			}
		}
		out.println("</pre> <p> The End </p> </body>");
		out.println("</html>");
		
	}

}
